package com.hualu.xmlCode.webapp;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hualu.xmlCode.model.AlbumViewSearchCondition;
import com.hualu.xmlCode.model.AppVideos;
import com.hualu.xmlCode.service.AlbumViewManager;

@Controller
public class AlbumViewController extends CdnBaseController {

    private transient final Log log = LogFactory.getLog(AlbumViewController.class);
    public static final String ALBUM_LIST = "albumList";
    private AlbumViewManager albumViewManager;

    public void setAlbumViewManager(AlbumViewManager albumViewManager) {
        this.albumViewManager = albumViewManager;
    }

    /**
     * 列表
     * 
     * @param request
     * @param response
     * @param albumViewSearchCondition
     * @param model
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/xmlCode/albumView")
    public String getAlbumViewListByCondition(HttpServletRequest request, HttpServletResponse response, HttpSession session,
            AlbumViewSearchCondition albumViewSearchCondition, Map model) {

        List albumList = new ArrayList();
        Locale locale = request.getLocale();

        AlbumViewSearchCondition searchCondition = (AlbumViewSearchCondition) session.getAttribute("searchCondition");

        // 初始化
        if (searchCondition == null && albumViewSearchCondition.getVideoType() == null) {
            albumViewSearchCondition.setVideoType(1);
        }

        // 添加完成
        if (searchCondition != null && albumViewSearchCondition.getVideoType() == null) {
            albumViewSearchCondition = searchCondition;
            String message = (String)session.getAttribute("appVideosOption");
            saveMessage(request, getText(message, locale));
            session.setAttribute("appVideosOption", "");
            session.setAttribute("searchCondition", "");
        }

        // 电影
        if (albumViewSearchCondition.getVideoType() == 1) {
            albumList = albumViewManager.getFilmsAlbumViewListByCondition(albumViewSearchCondition);
        }

        // 电视剧
        if (albumViewSearchCondition.getVideoType() == 2) {
            albumList = albumViewManager.getTvsAlbumViewListByCondition(albumViewSearchCondition);
        }

        // 片花
        if (albumViewSearchCondition.getVideoType() == 3) {
            albumList = albumViewManager.getOthersAlbumViewListByCondition(albumViewSearchCondition);
        }

        model.put("albumViewSearchCondition", albumViewSearchCondition);
        session.setAttribute("searchCondition", albumViewSearchCondition);

        request.setAttribute("albumList", albumList);
        request.setAttribute("appFilemsList", albumViewManager.getAppVideosList(1));
        request.setAttribute("appTvsList", albumViewManager.getAppVideosList(2));
        request.setAttribute("appOthersList", albumViewManager.getAppVideosList(3));
        
        
        
//        saveMessage(request, getText(msgKey, locale));
        
        return "/xmlCode/albumView";
    }

    /**
     * 获取appVideos对象，并保存
     * 
     * @param request
     * @param response
     * @param appVideos
     * @param model
     * @return
     */
    @RequestMapping(value = "/xmlCode/addAlbumView")
    public String addAlbumAppVideos(HttpServletRequest request, HttpServletResponse response, HttpSession session, AppVideos appVideos) {
        String appVideosOption = "";
        
        if (appVideos.getVideoId() == null) {
            log.info("我靠，视频id没拿着，保存失败");
            appVideosOption = "xmlCode.video.add.false";
        } else {
            appVideos.setFkAppId(1);
            this.albumViewManager.saveAppVideos(appVideos);
            appVideosOption = "xmlCode.video.add.success";
        }
        session.setAttribute("appVideosOption", appVideosOption);
        session.setAttribute("searchCondition", session.getAttribute("searchCondition"));
        return "redirect:/xmlCode/albumView.html";
    }
    
    /**
     * 删除
     * @param request
     * @param response
     * @param session
     * @param appVideos
     * @return
     */
    @RequestMapping(value = "/xmlCode/delAlbumView")
    public String delAppVideos (HttpServletRequest request, HttpServletResponse response, HttpSession session, AppVideos appVideos){
        String appVideosOption = "";
        
        if (appVideos.getId() == null) {
            log.info("我靠，视频id没拿着，删除失败");
            appVideosOption = "xmlCode.video.del.false";
        } else {
            this.albumViewManager.delAppVideos(appVideos.getId());
            appVideosOption = "xmlCode.video.del.success";
        }
        
        session.setAttribute("appVideosOption", appVideosOption);
        session.setAttribute("searchCondition", session.getAttribute("searchCondition"));
        return "redirect:/xmlCode/albumView.html";
    }
    
}